package com.odt.outdoor.fragments;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.widget.ImageView;
import android.widget.ZoomControls;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.model.LatLng;
import com.odt.outdoor.R;
import com.odt.outdoor.controller.trajectory.MapTypeActivity;
import com.odt.outdoor.core.BaseFragment;
import com.odt.outdoor.utils.Constants;
import com.odt.outdoor.widget.other.MyRel;
import com.odt.outdoor.widget.tab.ITabFragment;

import static android.app.Activity.RESULT_OK;


/**
 * 轨迹
 * Created by DELL on 2017/3/16.
 */

public class TrajectoryFragment extends BaseFragment implements ITabFragment,SwipeRefreshLayout.OnRefreshListener,MyRel.ClickLisetener {
    private MapView mMapView;
    private BaiduMap mBaiduMap;
    LocationClient mLocClient;
    private MyRel mZoomIn,mZoomOut,mTeam,mTrajectory,mLayer,mLoc;
    public MyLocationListenner myListener = new MyLocationListenner();
    private static final int TYPE = 100;
    @Override
    protected int getFragmentLayoutId() {
        return R.layout.fragment_trajectory_layout;
    }

    @Override
    protected void initView(View v) {
        findView(v);
        setView();
    }

    private void setView() {

    }

    private void findView(View v) {
        mMapView = (MapView) v.findViewById(R.id.bMapView);
        mZoomIn = (MyRel) v.findViewById(R.id.mZoomIn);
        mZoomOut = (MyRel) v.findViewById(R.id.mZoonOut);
        mTeam = (MyRel) v.findViewById(R.id.mTeam);
        mTrajectory = (MyRel) v.findViewById(R.id.mTrajectory);
        mLayer = (MyRel) v.findViewById(R.id.mLayer);
        mLoc = (MyRel) v.findViewById(R.id.mLoc);
        mBaiduMap = mMapView.getMap();
        setGoneLogo();
        startLoc();
        setClickListener();

    }

    private void setClickListener() {
        mZoomIn.setClick(this);
        mZoomOut.setClick(this);
        mTeam.setClick(this);
        mTrajectory.setClick(this);
        mLayer.setClick(this);
        mLoc.setClick(this);
    }

    private void setGoneLogo() {
        // 隐藏logo
        View child = mMapView.getChildAt(1);
        if (child != null && (child instanceof ImageView || child instanceof ZoomControls)){
            child.setVisibility(View.INVISIBLE);
        }
        //地图上比例尺
        mMapView.showScaleControl(false);
        // 隐藏缩放控件
        mMapView.showZoomControls(false);
    }

    private void startLoc() {
        mBaiduMap.setMyLocationEnabled(true);
        // 定位初始化
        mLocClient = new LocationClient(getContext());
        mLocClient.registerLocationListener(myListener);
        LocationClientOption option = new LocationClientOption();
        option.setOpenGps(true); // 打开gps
        option.setCoorType("bd09ll"); // 设置坐标类型
        option.setScanSpan(1000);
        mLocClient.setLocOption(option);
        mLocClient.start();
    }

    @Override
    public void setClick(String s) {
        switch (s) {
            case "mZoomIn":
                MapStatusUpdate zoomIn = MapStatusUpdateFactory.zoomIn();
                mBaiduMap.setMapStatus(zoomIn);
                break;
            case "mZoomOut":
                MapStatusUpdate zoomOut = MapStatusUpdateFactory.zoomOut();
                mBaiduMap.setMapStatus(zoomOut);
                break;
            case "mTeam":

                break;
            case "mTrajectory":

                break;
            case "mLayer":
                Intent intent = new Intent(getActivity(), MapTypeActivity.class);
                startActivityForResult(intent,TYPE);
                break;
            case "mLoc":

                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == TYPE && resultCode == RESULT_OK){
            int map_type = data.getIntExtra(Constants.MAP_TYPE, BaiduMap.MAP_TYPE_NORMAL);
            System.out.println(">>>>map_type"+map_type);
        }
    }

    @Override
    public void onRefresh() {

    }

    @Override
    public Fragment getFragment() {
        return this;
    }

    @Override
    public void onMenuItemClick(int id) {

    }

    @Override
    public void onPause() {
        super.onPause();
        mMapView.onPause();
    }

    @Override
    public void onResume() {
        super.onResume();
        mMapView.onResume();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mMapView.onDestroy();
    }
    boolean isFirstLoc = true;




    public class MyLocationListenner implements BDLocationListener{

        @Override
        public void onReceiveLocation(BDLocation location) {
            // map view 销毁后不在处理新接收的位置
            if (location == null || mMapView == null) {
                return;
            }
            MyLocationData locData = new MyLocationData.Builder()
                    .accuracy(location.getRadius())
                    // 此处设置开发者获取到的方向信息，顺时针0-360
                    .direction(100).latitude(location.getLatitude())
                    .longitude(location.getLongitude()).build();
            mBaiduMap.setMyLocationData(locData);
            if (isFirstLoc) {
                isFirstLoc = false;
                LatLng ll = new LatLng(location.getLatitude(),
                        location.getLongitude());
                MapStatus.Builder builder = new MapStatus.Builder();
                builder.target(ll).zoom(18.0f);
                mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build()));
            }
        }

        @Override
        public void onConnectHotSpotMessage(String s, int i) {

        }
    }

}
